package edu.neu.cloud_factory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.neu.cloud_factory.entity.po.RentRec;
import edu.neu.cloud_factory.entity.vo.RentRecVo;
import edu.neu.cloud_factory.service.RentRecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 租用记录 前端控制器
 * </p>
 *
 * @author halozhy
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/api/rr")
public class RentRecController {
    @Autowired
    RentRecService rentRecService;

    @GetMapping("list_by_device_id")
    public List<RentRecVo> listByDeviceId(long deviceId) {
        QueryWrapper<RentRec> rentRecQueryWrapper = new QueryWrapper<>();
        rentRecQueryWrapper.eq("r_device_id", deviceId);
        List<RentRec> rentRecList = rentRecService.list(rentRecQueryWrapper);

        List<RentRecVo> rentRecVoList = new ArrayList<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        rentRecList.forEach(rentRec -> {
            RentRecVo rentRecVo = new RentRecVo();
            rentRecVo.setDevice_id(deviceId);
            rentRecVo.setRent_time(rentRec.getRRentTime().format(dateTimeFormatter));
            rentRecVo.setId(rentRec.getRId());
            rentRecVo.setExpire_time(rentRec.getRExpireTime().format(dateTimeFormatter));
            if (rentRec.getRRecycleTime() != null){
                rentRecVo.setRecycle_time(rentRec.getRRecycleTime().format(dateTimeFormatter));
            }
            rentRecVo.setFactory_id(rentRec.getRFactoryId());
            rentRecVoList.add(rentRecVo);
        });

        return rentRecVoList;
    }
}

